var ITMChat_linecount = 0;
var ITMChat_lastMsgNum = 0;

function ITMChat_said(name, text, msgNum) 
{
	ITMChat_lastMsgNum = msgNum;
	var el = new Element('li', {});
	el.setHTML("<strong>"+name+":</strong> " + text);
	var ul = $('chatlist');
	var scrl = ITMChat_shouldScroll(ul);
	ITMChat_removeOldestIfNeeded();
	el.injectInside(ul);
	if (scrl) ITMChat_scrollToBottom(ul);
	Cookie.set('ChatCookie', $('chatlist').innerHTML);
	Cookie.set('ChatLastMsgNum', ITMChat_lastMsgNum);
	Cookie.set('ChatLineCount', ITMChat_linecount);
}

function ITMChat_systemMsg(msg, msgNum)
{
	ITMChat_lastMsgNum = msgNum;
	var el = new Element('li', {});
	el.setHTML("<small><strong style='color: #777777;'>"+msg+"</strong></small>");	
	var ul = $('chatlist');
	var scrl = ITMChat_shouldScroll(ul);
	ITMChat_removeOldestIfNeeded();
	el.injectInside(ul);
	if (scrl) ITMChat_scrollToBottom(ul);
	Cookie.set('ChatCookie', $('chatlist').innerHTML);
	Cookie.set('ChatLastMsgNum', ITMChat_lastMsgNum);
	Cookie.set('ChatLineCount', ITMChat_linecount);
}

function ITMChat_joined(name) {
	var el = new Element('li', {});
	el.setHTML("<small><em>"+name+"</em> joined</small>");
	var ul = $('chatlist');
	var scrl = ITMChat_shouldScroll(ul);
	ITMChat_removeOldestIfNeeded();
	el.injectInside(ul);
	if (scrl) ITMChat_scrollToBottom(ul);
}

function ITMChat_onEnter(e, inp) 
{
	if (e.keyCode == 13)
	{
		if (inp.value.trim() != '')
		{
			JsMain.say();
		}
		inp.value = '';
		e.preventDefault();
	}
}

function ITMChat_sayString(words) 
{
	JsMain.sayString(words);
}

function ITMChat_pageLoad()
{
	var savedCookie = Cookie.get('ChatCookie');
	if (savedCookie) {
		$('chatwrapper').addClass('on');
		$('chatwrapper').removeClass('idle');
		$('chatlist').setHTML( Cookie.get('ChatCookie') );
		ITMChat_linecount = parseInt(Cookie.get('ChatLineCount')); 
		(function(){
			JsMain.join( Cookie.get('ChatLastMsgNum')); 
			$('chat-join').setStyle('display', 'none');
			$('chat-options').setStyle('display', 'block');
			$('itmchat_input').setStyle('display', 'block');
		}).delay(1200);
		
		var ul = $('chatlist');
		ITMChat_scrollToBottom(ul);
	}
	else
	{
		$('chatwrapper').addClass('off');
		$('chatwrapper').removeClass('idle');		
	}
}

function ITMChat_leave()
{
	ITMChat_linecount = 0;
	ITMChat_lastMsgNum = 0;

	Cookie.remove('ChatCookie');
	Cookie.remove('ChatLastMsgNum');
	Cookie.remove('ChatLineCount');
	JsMain.leave();
	$('chat-join').setStyle('display', 'block');
	$('chat-options').setStyle('display', 'none');
	$('itmchat_input').setStyle('display', 'none');
	$('chatlist').innerHTML = '';
	$('chatwrapper').addClass('off');
	$('chatwrapper').removeClass('on');
}

function ITMChat_join()
{
	JsMain.join(0);
	$('chat-join').setStyle('display', 'none');
	$('chat-options').setStyle('display', 'block');
	$('itmchat_input').setStyle('display', 'block');
	$('chatwrapper').addClass('on');
	$('chatwrapper').removeClass('off');
	$('itmchat_input').focus();
	/*var el = new Element('li', {});
	el.setHTML("<small><em>pozdravljen-a na Chatu</em></small>");
	var ul = $('chatlist');
	el.injectInside(ul);
	ITMChat_scrollToBottom(ul);*/
}

function ITMChat_missed(msgs_)
{
	msgs_.forEach(function(value, key){
		//alert(value);
		var m = value.split('|||');
		ITMChat_said(m[0], m[1], parseInt(m[2]))
	});
}

function ITMChat_left(name) {
	var el = new Element('li', {});
	el.setHTML("<small><em>"+name+"</em> left</small>");	
	var ul = $('chatlist');
	var scrl = ITMChat_shouldScroll(ul);
	ITMChat_removeOldestIfNeeded();
	el.injectInside(ul);
	if (scrl) ITMChat_scrollToBottom(ul);
}

function ITMChat_welcome(msg, curMsgNum)
{
	ITMChat_lastMsgNum = curMsgNum;
	var el = new Element('li', {});
	el.setHTML("<small><em>" + msg + "</em></small>");	
	var ul = $('chatlist');
	var scrl = ITMChat_shouldScroll(ul);
	ITMChat_removeOldestIfNeeded();
	el.injectInside(ul);
	if (scrl) ITMChat_scrollToBottom(ul);
	Cookie.set('ChatCookie', $('chatlist').innerHTML);
	Cookie.set('ChatLastMsgNum', ITMChat_lastMsgNum);
	Cookie.set('ChatLineCount', ITMChat_linecount);
}

function ITMChat_getAndClearSayText() { return $('itmchat_input').value; }

function ITMChat_removeOldestIfNeeded()
{
	if (ITMChat_linecount > 30) 
	{
		$('chatlist').getFirst().remove();
	}
	else 
	{
		ITMChat_linecount ++;
	}
}

function ITMChat_scrollToBottom(ul)
{
	ul.scrollTop = ul.scrollHeight;
}

function ITMChat_shouldScroll(ul)
{
	return ul.scrollHeight - ul.scrollTop < 340;
}

window.addEvent('domready', function() { ITMChat_pageLoad(); });